package com.englishtest.city.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.util.WebUtils;

import com.englishtest.base.EnglishtestException;
import com.englishtest.base.GlobalConstant;
import com.englishtest.base.PageHelper;
import com.englishtest.base.Paginater;
import com.englishtest.city.service.CityService;
import com.englishtest.city.service.CityServiceFactory;
import com.englishtest.pojos.City;
import com.englishtest.pojos.College;
import com.englishtest.pojos.Fieldtime;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CollegeAction extends ActionSupport {
	private College college;
	private Fieldtime fieldtime;
	
	public String execute() {
		return "";
	}
	
	public String listCollege() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String searchWord = ServletRequestUtils.getStringParameter(request, "searchWord");
		Paginater paginater = PageHelper.getDefaultPaginater(request);
		
		CityService cityService = CityServiceFactory.getCityService();
		List collegeList = cityService.searchCollege(searchWord, paginater);
		ActionContext.getContext().put("collegeList", collegeList);
		ActionContext.getContext().put("pageProperty", PageHelper.getPageProperty(paginater));
		return "listCollege";
	}
	
	public String saveCollege() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		College c = getCollegeFromId(ServletRequestUtils.getStringParameter(request, "collegeId"));
		CityService cityService = CityServiceFactory.getCityService();
		if(WebUtils.hasSubmitParameter(request, "save")) {
			if(c == null) {
				c = new College();
			}
			Integer cityId = ServletRequestUtils.getIntParameter(request, "cityId");
			City city = cityService.getCity(cityId);
			c.setCity(city);
			c.setCollegeName(college.getCollegeName());
			c.setCollegeCode(college.getCollegeCode());
			c.setCollegePhone(college.getCollegePhone());
			c.setCollegeAddress(college.getCollegeAddress());
			c.setHoldCount(college.getHoldCount());
			c.setRemark(college.getRemark());
			
			try {
				cityService.saveCollege(c);
			} catch (Exception e) {
				e.printStackTrace();
				ServletActionContext.getResponse().getWriter().write("0");
				return null;
			}			
			ServletActionContext.getResponse().getWriter().write(String.valueOf(c.getId()));
			return null;
		}
		if(c != null) {
			ActionContext.getContext().put("college", c);
		}
		List cityList = cityService.searchCity(null, GlobalConstant.BOOLEAN_ALL, null);
		ActionContext.getContext().put("cityList", cityList);
		return "saveCollege";
	}
	
	public String listCollegeField() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String searchWord = ServletRequestUtils.getStringParameter(request, "searchWord");
		int isValid = ServletRequestUtils.getIntParameter(request, "isValid", GlobalConstant.BOOLEAN_ALL);
		Paginater paginater = PageHelper.getDefaultPaginater(request);
		
		CityService cityService = CityServiceFactory.getCityService();
		List fieldList = cityService.searchCollegeField(searchWord, isValid, paginater);
		ActionContext.getContext().put("fieldList", fieldList);
		ActionContext.getContext().put("pageProperty", PageHelper.getPageProperty(paginater));
		return "listCollegeField";
	}
	
	public String saveCollegeField() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Fieldtime c = getCollegeFieldFromId(ServletRequestUtils.getStringParameter(request, "collegeFieldId"));
		CityService cityService = CityServiceFactory.getCityService();
		if(WebUtils.hasSubmitParameter(request, "save")) {
			if(c == null) {
				c = new Fieldtime();
			}
			Integer collegeId = ServletRequestUtils.getIntParameter(request, "collegeId");
			College college = cityService.getCollege(collegeId);
			c.setFtName(fieldtime.getFtName());
			c.setFtCode(fieldtime.getFtCode());
			c.setCollege(college);
			c.setFtCount(fieldtime.getFtCount());
			c.setIfValid(fieldtime.getIfValid());
			
			try {
				cityService.saveFieldtime(c);
			} catch (Exception e) {
				e.printStackTrace();
				ServletActionContext.getResponse().getWriter().write("0");
				return null;
			}			
			ServletActionContext.getResponse().getWriter().write(String.valueOf(c.getId()));
			return null;
		}
		if(c != null) {
			ActionContext.getContext().put("fieldtime", c);
		}
		List collegeList = cityService.searchCollege(null, null);
		ActionContext.getContext().put("collegeList", collegeList);
		return "saveCollegeField";
	}
	
	private College getCollegeFromId(String collegeId) throws EnglishtestException {
		if(StringUtils.isBlank(collegeId)) return null;
		CityService cityService = CityServiceFactory.getCityService();
		return cityService.getCollege(Integer.decode(collegeId));
	}
	
	private Fieldtime getCollegeFieldFromId(String collegeFieldId) throws EnglishtestException {
		if(StringUtils.isBlank(collegeFieldId)) return null;
		CityService cityService = CityServiceFactory.getCityService();
		return cityService.getCollegeField(Integer.decode(collegeFieldId));
	}

	public College getCollege() {
		return college;
	}

	public void setCollege(College college) {
		this.college = college;
	}

	public Fieldtime getFieldtime() {
		return fieldtime;
	}

	public void setFieldtime(Fieldtime fieldtime) {
		this.fieldtime = fieldtime;
	}
	
}
